package com.fjb.service.product.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonPagesResult;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.JsonTotalResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.entity.TotalResult;
import com.fjb.common.enums.product.AttrType;
import com.fjb.common.enums.product.DealType;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.mapper.log.ProductPhoneLogMapper;
import com.fjb.mapper.product.ProductAttributeMapper;
import com.fjb.mapper.product.ProductAttributeSkuMapper;
import com.fjb.mapper.product.ProductInfoMapper;
import com.fjb.pojo.log.ProductPhoneLog;
import com.fjb.pojo.product.ProductAttribute;
import com.fjb.pojo.product.ProductAttributeSku;
import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.GetProductPhoneVo;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.service.product.ProductInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * <p>
 * 产品信息 服务实现类
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {
	
	@Autowired
	private ProductInfoMapper productInfoMapper;
	@Autowired
	private ProductAttributeMapper productAttributeMapper;
	@Autowired
	private ProductAttributeSkuMapper productAttributeSkuMapper;
	@Autowired
	private ProductPhoneLogMapper productPhoneLogMapper;
	
	@Override
	public JsonTotalResult<List<ProductInfoPageVo>> selectPageList(ProductInfoQuery productQuery, PageQuery pageQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectPageList(productQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		return new JsonTotalResult<List<ProductInfoPageVo>>(infoList, HttpCode.SUCCESS, pageInfo.getTotal());
	}

	@Override
	public TotalResult<List<ProductInfoPageVo>> selectIndexList(ProductInfoQuery productInfoQuery,
			PageQuery pageQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectPageList(productInfoQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		return new TotalResult<List<ProductInfoPageVo>>(infoList,pageInfo.getTotal(),pageInfo.getPages());
	}

	@Override
	public JsonPagesResult<List<ProductInfoPageVo>> selectPagesList(PageQuery pageQuery,
			ProductInfoQuery productQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		productQuery.setProductStatus(ProductStatus.STATUS_1.getCode());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectPageList(productQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		if(infoList.size()>0) {		
			for (ProductInfoPageVo in : infoList) {
				// 主文件
				String mainFileUrl = in.getMainFileUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					in.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}	
				// 状态msg
				Integer status = in.getProductStatus();
				String statusMsg = ProductStatus.getStatusMsg(status);
				if(StringUtils.isNotBlank(statusMsg)) {
					in.setProductStatusMsg(statusMsg);
				}	
				// 交易类型msg
				Integer dealType = in.getDealType();
				String typeMsg = DealType.getTypeMsg(dealType);
				if(StringUtils.isNotBlank(typeMsg)) {
					in.setDealTypeMsg(typeMsg);	
				}
			}
		}
		
		return new JsonPagesResult<List<ProductInfoPageVo>>(infoList,HttpCode.SUCCESS,pageInfo.getTotal(),pageInfo.getPages());
	}

	@Transactional
	@Override
	public JsonResult<Integer> addInfo(ProductInfo pi, String attrName) {
		Integer mainUserId = pi.getMainUserId();
		Integer userId = pi.getUserId();
		LocalDateTime createTime = pi.getCreateTime();
		BigDecimal currentPrice = pi.getCurrentPrice();
		Integer productCount = pi.getProductCount();
		
		productInfoMapper.insert(pi);
		Integer productId = pi.getId();
		
		Date date = new Date();
		String attrTitleCode = new SimpleDateFormat("yyMMddHHmmss").format(date);
		String attrCodeOne = attrTitleCode+userId;	
		String attrCodeTwo = "0";
		Integer attrLevel = 1;
		// 1级属性标题	
		ProductAttribute attr1 = new ProductAttribute();
		attr1.setProductId(productId);
		attr1.setAttrLevel(attrLevel);
		attr1.setAttrName("属性");
		attr1.setMainUserId(mainUserId);
		attr1.setAttrType(AttrType.TYPE_1.getCode());
		attr1.setAttrTitleCode(attrTitleCode);
		attr1.setCreateTime(createTime);
		attr1.setCreateUserId(userId);
		
		// 1级属性名称
		ProductAttribute attr2 = new ProductAttribute();
		attr2.setProductId(productId);
		attr2.setAttrLevel(attrLevel);
		attr2.setAttrName(attrName);
		attr2.setMainUserId(mainUserId);
		attr2.setAttrType(AttrType.TYPE_2.getCode());
		attr2.setAttrTitleCode(attrTitleCode);
		attr2.setAttrCodeOne(attrCodeOne);
		attr2.setAttrCodeTwo(attrCodeTwo);
		attr2.setCreateTime(createTime);
		attr2.setCreateUserId(userId);
		
		List<ProductAttribute> listBatch = new ArrayList<ProductAttribute>();
		listBatch.add(attr1);
		listBatch.add(attr2);
		productAttributeMapper.addBatch(listBatch);
		
		// 属性sku
		ProductAttributeSku sku = new ProductAttributeSku();
		sku.setProductId(productId);
		sku.setMainUserId(mainUserId);
		sku.setAttrTitleCode(attrTitleCode);
		sku.setAttrCodeOne(attrCodeOne);
		sku.setAttrCodeTwo(attrCodeTwo);
		sku.setSkuCount(productCount);
		sku.setSkuPrice(currentPrice);
		sku.setCreateUserId(userId);
		sku.setCreateTime(createTime);
		sku.setAttrLevel(attrLevel);
		productAttributeSkuMapper.insert(sku);	
		return new JsonResult<Integer>(null, HttpCode.SUCCESS);
	}

	@Override
	public JsonPagesResult<List<ProductInfoPageVo>> selectSearchPageList(ProductInfoQuery infoQuery,
			PageQuery pageQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectSearchPageList(infoQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		if(infoList.size()>0) {	
			for (ProductInfoPageVo in : infoList) {
				String mainFileUrl = in.getMainFileUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					in.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}	
			}
		}
		return new JsonPagesResult<List<ProductInfoPageVo>>(infoList, HttpCode.SUCCESS, pageInfo.getTotal(), pageInfo.getPages());
	}

	@Override
	public ProductInfoPageVo selectGetOneById(Integer productId) {
		return productInfoMapper.selectGetOneById(productId);
	}

	@Transactional
	@Override
	public JsonResult<String> getProductPhone(Integer mainUserId, Integer userId, Integer productId) {
		// 获得产品手机号
		GetProductPhoneVo productPhone = productInfoMapper.getProductPhone(productId);
		if(productPhone==null) {
			return new JsonResult<String>(null, HttpCode.PARAM_VERIFICATION);
		}
		LocalDateTime localDateTime = LocalDateTime.now();
		String phone = productPhone.getPhone();
		Integer productUserId = productPhone.getUserId();
		// 我想要 次数加  1		
		productInfoMapper.updateWantCountAdd(productId);
		
		// 添加 我想要日志 
		ProductPhoneLog pl = new ProductPhoneLog();
		pl.setProductId(productId);
		pl.setProductUserId(productUserId);
		pl.setLookUserId(userId);
		pl.setPhone(phone);
		pl.setCreateTime(localDateTime);
		pl.setCreateUserId(userId);
		productPhoneLogMapper.insert(pl);
		return new JsonResult<String>(phone, HttpCode.SUCCESS);
	}

	@Override
	public int updateViewCountAdd(Integer productId) {
		return productInfoMapper.updateViewCountAdd(productId);
	}
	
	@Transactional
	@Override
	public JsonResult<Integer> updateInfo(ProductInfo pi, String attrName) {
		Integer mainUserId = pi.getMainUserId();
		Integer userId = pi.getUserId();
		LocalDateTime updateTime = pi.getUpdateTime();
		BigDecimal currentPrice = pi.getCurrentPrice();
		Integer productCount = pi.getProductCount();
		
		productInfoMapper.updateById(pi);
		Integer productId = pi.getId();
		
		// 删除属性
		QueryWrapper<ProductAttribute> attrQueryWrapper = new QueryWrapper<ProductAttribute>();
		attrQueryWrapper.eq("product_id", productId);
		productAttributeMapper.delete(attrQueryWrapper);
		Date date = new Date();
		String attrTitleCode = new SimpleDateFormat("yyMMddHHmmss").format(date);
		String attrCodeOne = attrTitleCode+userId;	
		String attrCodeTwo = "0";
		Integer attrLevel = 1;
		// 1级属性标题	
		ProductAttribute attr1 = new ProductAttribute();
		attr1.setProductId(productId);
		attr1.setAttrLevel(attrLevel);
		attr1.setAttrName("属性");
		attr1.setMainUserId(mainUserId);
		attr1.setAttrType(AttrType.TYPE_1.getCode());
		attr1.setAttrTitleCode(attrTitleCode);
		attr1.setCreateTime(updateTime);
		attr1.setCreateUserId(userId);
		
		// 1级属性名称
		ProductAttribute attr2 = new ProductAttribute();
		attr2.setProductId(productId);
		attr2.setAttrLevel(attrLevel);
		attr2.setAttrName(attrName);
		attr2.setMainUserId(mainUserId);
		attr2.setAttrType(AttrType.TYPE_2.getCode());
		attr2.setAttrTitleCode(attrTitleCode);
		attr2.setAttrCodeOne(attrCodeOne);
		attr2.setAttrCodeTwo(attrCodeTwo);
		attr2.setCreateTime(updateTime);
		attr2.setCreateUserId(userId);
		
		List<ProductAttribute> listBatch = new ArrayList<ProductAttribute>();
		listBatch.add(attr1);
		listBatch.add(attr2);
		productAttributeMapper.addBatch(listBatch);
		
		// 删除sku属性
		QueryWrapper<ProductAttributeSku> skuQueryWrapper = new QueryWrapper<ProductAttributeSku>();
		skuQueryWrapper.eq("product_id", productId);
		productAttributeSkuMapper.delete(skuQueryWrapper);
		// 属性sku
		ProductAttributeSku sku = new ProductAttributeSku();
		sku.setProductId(productId);
		sku.setMainUserId(mainUserId);
		sku.setAttrTitleCode(attrTitleCode);
		sku.setAttrCodeOne(attrCodeOne);
		sku.setAttrCodeTwo(attrCodeTwo);
		sku.setSkuCount(productCount);
		sku.setSkuPrice(currentPrice);
		sku.setCreateUserId(userId);
		sku.setCreateTime(updateTime);
		sku.setAttrLevel(attrLevel);
		productAttributeSkuMapper.insert(sku);	
		return new JsonResult<Integer>(null, HttpCode.SUCCESS);
	}

	@Override
	public JsonPagesResult<List<ProductInfoPageVo>> selectMyPagesList(PageQuery pageQuery,
			ProductInfoQuery productQuery) {
		PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
		List<ProductInfoPageVo> infoList = productInfoMapper.selectMyPagesList(productQuery);
		PageInfo<ProductInfoPageVo> pageInfo = new PageInfo<ProductInfoPageVo>(infoList);
		if(infoList.size()>0) {		
			for (ProductInfoPageVo in : infoList) {
				// 主文件
				String mainFileUrl = in.getMainFileUrl();
				if(StringUtils.isNoneBlank(mainFileUrl)) {
					in.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
				}	
				// 状态msg
				Integer status = in.getProductStatus();
				String statusMsg = ProductStatus.getStatusMsg(status);
				if(StringUtils.isNotBlank(statusMsg)) {
					in.setProductStatusMsg(statusMsg);
				}	
				// 交易类型msg
				Integer dealType = in.getDealType();
				String typeMsg = DealType.getTypeMsg(dealType);
				if(StringUtils.isNotBlank(typeMsg)) {
					in.setDealTypeMsg(typeMsg);	
				}
			}
		}
		
		return new JsonPagesResult<List<ProductInfoPageVo>>(infoList,HttpCode.SUCCESS,pageInfo.getTotal(),pageInfo.getPages());
	}
}
